package cn.catsdad.util;

import cn.catsdad.constant.iConstant.ICommonWarningType;
import cn.catsdad.exception.BaseException;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * json工具
 *
 * @author xuyl
 * @version 1.0 2024/7/14
 */
@SuppressWarnings(ICommonWarningType.UNUSED)
public class JsonUtil {

    private static final Logger log = LoggerFactory.getLogger(JsonUtil.class);

    public static String toJsonStr(Object o) {
        String result = "";
        if (o != null) {
            try {
                result = JSONUtil.toJsonStr(o);
            } catch (Exception e) {
                throw new BaseException("对象序列化失败，错误信息：" + e.getMessage());
            }

        }
        return result;
    }

    public static <T> T deSerialize(String json, Class<T> tClass) {
        T t = null;
        if (StringUtil.isNotEmpty(json)) {
            t = JSONUtil.toBean(json, tClass);
        }
        return t;
    }

    public static <T> List<T> deSerialize(List<String> json, Class<T> tClass) {
        List<T> result = new ArrayList<>(json.size());
        if (StringUtil.isNotEmpty(json)) {
            for (String s : json) {
                if (StringUtil.isNotEmpty(s)) {
                    result.add(deSerialize(s, tClass));
                }
            }
        }
        return result;
    }

    /**
     * 反序列化忽略属性大小写
     *
     * @param json   json
     * @param tClass 反序列化目标类型
     * @return 反序列化对象
     */
    public static <T> T deSerializeIgnoreUppercase(String json, Class<T> tClass) {
        T result;
        JSONObject jsonObject = JSONUtil.parseObj(json);
        Field[] fields = ReflectUtil.getFields(tClass);
        Set<String> jsonKeys = jsonObject.keySet();
        try {
            result = tClass.newInstance();
            for (Field field : fields) {
                field.setAccessible(true);
                Optional<String> key = jsonKeys.stream().filter(k -> field.getName().equalsIgnoreCase(k)).findFirst();
                key.ifPresent(k -> {
                    try {
                        field.set(result, jsonObject.get(k, field.getType()));
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                });
            }
        } catch (Exception e) {
            throw new BaseException("反序列化对象必须有无参构造！");
        }
        return result;
    }

    public static <T> List<T> deSerializeList(String json, Class<T> tClass) {
        List<T> result = new ArrayList<>();
        if (StringUtil.isNotEmpty(json)) {
            result = JSONUtil.toList(json, tClass);
        }
        return result;
    }

    public static <T> String serialize(T t) {
        return toJsonStr(t);
    }

    public static <T> List<String> serializeList(List<T> list) {
        List<String> result = new ArrayList<>();
        if (StringUtil.isNotEmpty(list)) {
            for (T t : list) {
                result.add(toJsonStr(t));
            }
        }
        return result;
    }


}
